
import { VisualHelper } from "utils/visual/visual";
import { AdaptationLayer } from "./AdaptationLayer/AdaptationLayer";
import { TaskManager } from "./DecisionMaking/TaskManager";
import { WorkManager } from "./Execution/WorkManager";
import { StationNetwork } from "./Path/StationNetwork";
import { WorldState } from "./WorldState/worldstate";
 
// import profiler from "screeps-profiler";
// profiler.registerClass(WorldState,"WorldState")
// profiler.registerClass(TaskManager,"TaskManager")
// profiler.registerClass(AdaptationLayer,"AdaptationLayer")
// profiler.registerClass(WorkManager,"WorkManager")
/**
 * 游戏主循环
 */
/*@profile*/
export class GameLoop 
{
    private worldstate:WorldState;
    private decision:TaskManager;
    // private taskDispatch:TaskManager;
    private workerManager:WorkManager;
    private adaptation:AdaptationLayer;
    constructor()
    {
        this.worldstate = new WorldState();
        this.decision = new TaskManager();
        // this.taskDispatch = new TaskManager();
        this.workerManager =  new WorkManager();
        this.adaptation = new AdaptationLayer();
    }
    /**
     * 初始化
     */
    public init()
    {
        // Memory.rooms={};
       
        // global._move_intents={};
        this.decision.init(this.worldstate);
        // this.decision.upadteListen();
        this.worldstate.init();
       
        this.adaptation.init(this.worldstate);
        this.workerManager.init();
       
    }

    /**
     * 每Tick运行
     */
    public run()
    { 
        this.decision.upadteListen()
        StationNetwork.getInstance.update();
        this.worldstate.tickStart();
        this.adaptation.run();
        this.workerManager.run( this.worldstate);

        // this.drawPath();
        this.worldstate.tickEnd();
 
    }
    private drawPath()
    {
       // PathFinder.search(new RoomPosition(16,35,'E53S58'),new RoomPosition(15,44,'E53S58'),)
        // Game.rooms["E57S54"].getTaskByType("creep",(e=>e.data.type=="build")).forEach(t=>t.data.required[0][1]=1)
        if(Game.cpu.bucket>2000)
        {
            const cache = (StationNetwork.getInstance.PathCache as any)._cache as Record<string,RoomPosition[]>;
            for(let k in cache)
            {
                // cache
                VisualHelper.drawPath( cache[k]);
            }
        }
    }
}